using System;
using System.Collections;
using System.Xml;

namespace UNLV.IAP.WebControls
{
	/// <summary>
	/// Strongly-typed collection of SqlWhereBuilderCondition objects.
	/// </summary>
	[Serializable]
	public class SqlWhereBuilderConditionCollection : CollectionBase
	{

		public SqlWhereBuilderCondition this[ int index ]  
		{
			get  
			{
				return( (SqlWhereBuilderCondition) List[index] );
			}
			set  
			{
				List[index] = value;
			}
		}

		public int Add( SqlWhereBuilderCondition value )  
		{
			return( List.Add( value ) );
		}

		public int IndexOf( SqlWhereBuilderCondition value )  
		{
			return( List.IndexOf( value ) );
		}

		public void Insert( int index, SqlWhereBuilderCondition value )  
		{
			List.Insert( index, value );
		}

		public void Remove( SqlWhereBuilderCondition value )  
		{
			List.Remove( value );
		}

		public bool Contains( SqlWhereBuilderCondition value )  
		{
			// If value is not of type SqlWhereBuilderCondition, this will return false.
			return( List.Contains( value ) );
		}

		protected override void OnInsert( int index, Object value )  
		{
			if ( value.GetType() != Type.GetType("UNLV.IAP.WebControls.SqlWhereBuilderCondition") )
				throw new ArgumentException( "value must be of type SqlWhereBuilderCondition.", "value" );
		}

		protected override void OnRemove( int index, Object value )  
		{
			if ( value.GetType() != Type.GetType("UNLV.IAP.WebControls.SqlWhereBuilderCondition") )
				throw new ArgumentException( "value must be of type SqlWhereBuilderCondition.", "value" );
		}

		protected override void OnSet( int index, Object oldValue, Object newValue )  
		{
			if ( newValue.GetType() != Type.GetType("UNLV.IAP.WebControls.SqlWhereBuilderCondition") )
				throw new ArgumentException( "newValue must be of type SqlWhereBuilderCondition.", "newValue" );
		}

		protected override void OnValidate( Object value )  
		{
			if ( value.GetType() != Type.GetType("UNLV.IAP.WebControls.SqlWhereBuilderCondition") )
				throw new ArgumentException( "value must be of type SqlWhereBuilderCondition." );
		}

		public void DeserializeFromNode(XmlNode n)
		{
			//<conditions>...</conditions>
			// clear any existing nodes first
			this.Clear();
			
			// select all <condition> child nodes
			XmlNodeList nodes = n.SelectNodes("condition");

			// loop through the list
			foreach (XmlNode nC in nodes)
			{
				this.Add(new SqlWhereBuilderCondition(nC));
			}
		}

		public SqlWhereBuilderConditionCollection(XmlNode n) : base()
		{
			this.DeserializeFromNode(n);
		}

		public SqlWhereBuilderConditionCollection() : base()
		{
		}

		public override bool Equals(object obj)
		{
			// compare by reference first...
			if (base.Equals(obj)) return true;

			// if not the same reference, then look at the internal properties
			// for comparison
			bool retValue = true;

			SqlWhereBuilderConditionCollection v = obj as SqlWhereBuilderConditionCollection;
			if (!(object.ReferenceEquals(v, null)))
			{
				if (v.Count != this.Count)
					retValue = false;
				else
				{
					IEnumerator vEnum = v.GetEnumerator();
					IEnumerator tEnum = this.GetEnumerator();

					while(vEnum.MoveNext() && tEnum.MoveNext())
					{
						if ( !(vEnum.Current.Equals(tEnum.Current)) )
						{
							retValue = false;
							break;
						}
					}
				}
					
			}
			else
				retValue = false;

			return retValue;
			
		}

		public override int GetHashCode()
		{
			int hashCode = 0;

			foreach (object o in this)
			{
				hashCode ^= o.GetHashCode();
			}

			return hashCode;
		}

		public static bool operator ==(SqlWhereBuilderConditionCollection v1, SqlWhereBuilderConditionCollection v2)
		{
			if ( (object.ReferenceEquals(v1, null))
				&& (object.ReferenceEquals(v2, null)) )
				return true;
			else if ( (object.ReferenceEquals(v1, null)) )
				return false;
			else return v1.Equals(v2);
		}

		public static bool operator !=(SqlWhereBuilderConditionCollection v1, SqlWhereBuilderConditionCollection v2)
		{
			if ( (object.ReferenceEquals(v1, null))
				&& (object.ReferenceEquals(v2, null)) )
				return false;
			else if ( (object.ReferenceEquals(v1, null)) )
				return true;
			else return !(v1.Equals(v2));
		}


	}
}
